<?php

/**
 * 基础模型封装
 */
namespace app\system\model;

use app\base\model\BaseModel;

class SystemModel extends BaseModel {


    /**
     * 获取列表
     * @return array
     */
    public function loadList() {
        $params = func_get_args();
        $where = !empty($params[0]) ? $params[0] : [];
        $limit = !empty($params[1]) ? $params[1] : 0;
        $order = !empty($params[2]) ? $params[2] : null;
        return $this->where($where)->limit($limit)->order($order)->select();
    }

    /**
     * 获取数量
     * @param array $where
     * @return mixed
     */
    public function countList($where = []) {
        return $this->where($where)->count();
    }

    /**
     * 获取信息
     * @param $id
     * @return array|bool
     */
    public function getInfo($id) {
        if (empty($this->primary)) {
            return false;
        }
        $where = [];
        $where[$this->primary] = $id;
        return $this->getWhereInfo($where);
    }

    /**
     * 获取信息
     * @param $where
     * @return array
     */
    public function getWhereInfo($where) {
        return $this->where($where)->find();
    }

    /**
     * 保存信息
     * @param string $type
     * @param array $data
     * @return bool
     */
    public function saveData($type = 'add', $data = []) {
        $data = $this->create($data);
        if (!$data) {
            return false;
        }
        if (method_exists($this, '_saveBefore')) {
            $data = $this->_saveBefore($data);
        }
        if ($type == 'add') {
            if (method_exists($this, '_addBefore')) {
                $data = $this->_addBefore($data);
            }
            $id = $this->add($data);
            $data[$this->primary] = $id;
            if (!$id) {
                return false;
            }
            if (method_exists($this, '_saveAfter')) {
                if (!$this->_saveAfter($type, $data)) {
                    return false;
                }
            }
            return $id;
        }
        if ($type == 'edit') {
            if (method_exists($this, '_editBefore')) {
                $data = $this->_editBefore($data);
            }
            if (empty($data[$this->primary])) {
                return false;
            }
            if (!$this->edit($data)) {
                return false;
            }
            if (method_exists($this, '_saveAfter')) {
                if (!$this->_saveAfter($type, $data)) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /**
     * 删除信息
     * @param $id
     * @return bool
     */
    public function delData($id) {
        if (method_exists($this, '_delBefore')) {
            if(!$this->_delBefore($id)){
                return false;
            }
        }
        $where = array();
        $where[$this->primary] = $id;
        if (!$this->where($where)->delete()) {
            return false;
        }
        if (method_exists($this, '_delAfter')) {
            if(!$this->_delAfter($id)){
                return false;
            }
        }
        return true;
    }

}